home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tcsh / dist / tc.str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  8.3 KB  |  407 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/tc.str.c,v 3.3 1991/10/12 04:23:51 christos Exp $ */
  2. /*
  3.  * tc.str.c: Short string package
  4.  *          This has been a lesson of how to write buggy code!
  5.  */
  6. /*-
  7.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38. #include "sh.h"
  39.  
  40. RCSID("$Id: tc.str.c,v 3.3 1991/10/12 04:23:51 christos Exp $")
  41.  
  42. #define MALLOC_INCR    128
  43.  
  44. #ifdef SHORT_STRINGS
  45. Char  **
  46. blk2short(src)
  47.     register char **src;
  48. {
  49.     size_t     n;
  50.     register Char **sdst, **dst;
  51.  
  52.     /*
  53.      * Count
  54.      */
  55.     for (n = 0; src[n] != NULL; n++);
  56.     sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
  57.  
  58.     for (; *src != NULL; src++)
  59.     *dst++ = SAVE(*src);
  60.     *dst = NULL;
  61.     return (sdst);
  62. }
  63.  
  64. char  **
  65. short2blk(src)
  66.     register Char **src;
  67. {
  68.     size_t     n;
  69.     register char **sdst, **dst;
  70.  
  71.     /*
  72.      * Count
  73.      */
  74.     for (n = 0; src[n] != NULL; n++);
  75.     sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
  76.  
  77.     for (; *src != NULL; src++)
  78.     *dst++ = strsave(short2str(*src));
  79.     *dst = NULL;
  80.     return (sdst);
  81. }
  82.  
  83. Char   *
  84. str2short(src)
  85.     register char *src;
  86. {
  87.     static Char *sdst;
  88.     static size_t dstsize = 0;
  89.     register Char *dst, *edst;
  90.  
  91.     if (src == NULL)
  92.     return (NULL);
  93.  
  94.     if (sdst == (NULL)) {
  95.     dstsize = MALLOC_INCR;
  96.     sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
  97.     }
  98.  
  99.     dst = sdst;
  100.     edst = &dst[dstsize];
  101.     while (*src) {
  102.     *dst++ = (Char) ((unsigned char) *src++);
  103.     if (dst == edst) {
  104.         dstsize += MALLOC_INCR;
  105.         sdst = (Char *) xrealloc((ptr_t) sdst,
  106.                      (size_t) dstsize * sizeof(Char));
  107.         edst = &sdst[dstsize];
  108.         dst = &edst[-MALLOC_INCR];
  109.     }
  110.     }
  111.     *dst = 0;
  112.     return (sdst);
  113. }
  114.  
  115. char   *
  116. short2str(src)
  117.     register Char *src;
  118. {
  119.     static char *sdst = NULL;
  120.     static size_t dstsize = 0;
  121.     register char *dst, *edst;
  122.  
  123.     if (src == NULL)
  124.     return (NULL);
  125.  
  126.     if (sdst == NULL) {
  127.     dstsize = MALLOC_INCR;
  128.     sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
  129.     }
  130.     dst = sdst;
  131.     edst = &dst[dstsize];
  132.     while (*src) {
  133.     *dst++ = (char) *src++;
  134.     if (dst == edst) {
  135.         dstsize += MALLOC_INCR;
  136.         sdst = (char *) xrealloc((ptr_t) sdst,
  137.                      (size_t) dstsize * sizeof(char));
  138.         edst = &sdst[dstsize];
  139.         dst = &edst[-MALLOC_INCR];
  140.     }
  141.     }
  142.     *dst = 0;
  143.     return (sdst);
  144. }
  145.  
  146. Char   *
  147. s_strcpy(dst, src)
  148.     register Char *dst, *src;
  149. {
  150.     register Char *sdst;
  151.  
  152.     sdst = dst;
  153.     while (*dst++ = *src++);
  154.     return (sdst);
  155. }
  156.  
  157. Char   *
  158. s_strncpy(dst, src, n)
  159.     register Char *dst, *src;
  160.     register size_t n;
  161. {
  162.     register Char *sdst;
  163.  
  164.     if (n == 0)
  165.     return(dst);
  166.  
  167.     sdst = dst;
  168.     do 
  169.     if ((*dst++ = *src++) == '\0') {
  170.         while (--n != 0)
  171.         *dst++ = '\0';
  172.         return(sdst);
  173.     }
  174.     while (--n != 0);
  175.     return (sdst);
  176. }
  177.  
  178. Char   *
  179. s_strcat(dst, src)
  180.     register Char *dst, *src;
  181. {
  182.     register short *sdst;
  183.  
  184.     sdst = dst;
  185.     while (*dst++);
  186.     --dst;
  187.     while (*dst++ = *src++);
  188.     return (sdst);
  189. }
  190.  
  191. #ifdef NOTUSED
  192. Char   *
  193. s_strncat(dst, src, n)
  194.     register Char *dst, *src;
  195.     register size_t n;
  196. {
  197.     register Char *sdst;
  198.  
  199.     if (n == 0) 
  200.     return (dst);
  201.  
  202.     sdst = dst;
  203.  
  204.     while (*dst++);
  205.     --dst;
  206.  
  207.     do 
  208.     if ((*dst++ = *src++) == '\0')
  209.         return(sdst);
  210.     while (--n != 0);
  211.  
  212.     *dst = '\0';
  213.     return (sdst);
  214. }
  215.  
  216. #endif
  217.  
  218. Char   *
  219. s_strchr(str, ch)
  220.     register Char *str;
  221.     int ch;
  222. {
  223.     do
  224.     if (*str == ch)
  225.         return (str);
  226.     while (*str++);
  227.     return (NULL);
  228. }
  229.  
  230. Char   *
  231. s_strrchr(str, ch)
  232.     register Char *str;
  233.     int ch;
  234. {
  235.     register Char *rstr;
  236.  
  237.     rstr = NULL;
  238.     do
  239.     if (*str == ch)
  240.         rstr = str;
  241.     while (*str++);
  242.     return (rstr);
  243. }
  244.  
  245. size_t
  246. s_strlen(str)
  247.     register Char *str;
  248. {
  249.     register size_t n;
  250.  
  251.     for (n = 0; *str++; n++);
  252.     return (n);
  253. }
  254.  
  255. int
  256. s_strcmp(str1, str2)
  257.     register Char *str1, *str2;
  258. {
  259.     for (; *str1 && *str1 == *str2; str1++, str2++);
  260.     /*
  261.      * The following case analysis is necessary so that characters which look
  262.      * negative collate low against normal characters but high against the
  263.      * end-of-string NUL.
  264.      */
  265.     if (*str1 == '\0' && *str2 == '\0')
  266.     return (0);
  267.     else if (*str1 == '\0')
  268.     return (-1);
  269.     else if (*str2 == '\0')
  270.     return (1);
  271.     else
  272.     return (*str1 - *str2);
  273. }
  274.  
  275. int
  276. s_strncmp(str1, str2, n)
  277.     register Char *str1, *str2;
  278.     register size_t n;
  279. {
  280.     if (n == 0)
  281.     return (0);
  282.     do {
  283.     if (*str1 != *str2) {
  284.         /*
  285.          * The following case analysis is necessary so that characters 
  286.          * which look negative collate low against normal characters
  287.          * but high against the end-of-string NUL.
  288.          */
  289.         if (*str1 == '\0')
  290.         return (-1);
  291.         else if (*str2 == '\0')
  292.         return (1);
  293.         else
  294.         return (*str1 - *str2);
  295.     }
  296.         if (*str1 == '\0')
  297.         return(0);
  298.     str1++, str2++;
  299.     } while (--n != 0);
  300.     return(0);
  301. }
  302.  
  303. Char   *
  304. s_strsave(s)
  305.     register Char *s;
  306. {
  307.     Char   *n;
  308.     register Char *p;
  309.  
  310.     if (s == 0)
  311.     s = STRNULL;
  312.     for (p = s; *p++;);
  313.     n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
  314.     while (*p++ = *s++);
  315.     return (n);
  316. }
  317.  
  318. Char   *
  319. s_strspl(cp, dp)
  320.     Char   *cp, *dp;
  321. {
  322.     Char   *ep;
  323.     register Char *p, *q;
  324.  
  325.     if (!cp)
  326.     cp = STRNULL;
  327.     if (!dp)
  328.     dp = STRNULL;
  329.     for (p = cp; *p++;);
  330.     for (q = dp; *q++;);
  331.     ep = (Char *) xmalloc((size_t)
  332.               (((p - cp) + (q - dp) - 1) * sizeof(Char)));
  333.     for (p = ep, q = cp; *p++ = *q++;);
  334.     for (p--, q = dp; *p++ = *q++;);
  335.     return (ep);
  336. }
  337.  
  338. Char   *
  339. s_strend(cp)
  340.     register Char *cp;
  341. {
  342.     if (!cp)
  343.     return (cp);
  344.     while (*cp)
  345.     cp++;
  346.     return (cp);
  347. }
  348.  
  349. Char   *
  350. s_strstr(s, t)
  351.     register Char *s, *t;
  352. {
  353.     do {
  354.     register Char *ss = s;
  355.     register Char *tt = t;
  356.  
  357.     do
  358.         if (*tt == '\0')
  359.         return (s);
  360.     while (*ss++ == *tt++);
  361.     } while (*s++ != '\0');
  362.     return (NULL);
  363. }
  364.  
  365. #endif                /* SHORT_STRINGS */
  366.  
  367. char   *
  368. short2qstr(src)
  369.     register Char *src;
  370. {
  371.     static char *sdst = NULL;
  372.     static size_t dstsize = 0;
  373.     register char *dst, *edst;
  374.  
  375.     if (src == NULL)
  376.     return (NULL);
  377.  
  378.     if (sdst == NULL) {
  379.     dstsize = MALLOC_INCR;
  380.     sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
  381.     }
  382.     dst = sdst;
  383.     edst = &dst[dstsize];
  384.     while (*src) {
  385.     if (*src & QUOTE) {
  386.         *dst++ = '\\';
  387.         if (dst == edst) {
  388.         dstsize += MALLOC_INCR;
  389.         sdst = (char *) xrealloc((ptr_t) sdst,
  390.                      (size_t) dstsize * sizeof(char));
  391.         edst = &sdst[dstsize];
  392.         dst = &edst[-MALLOC_INCR];
  393.         }
  394.     }
  395.     *dst++ = (char) *src++;
  396.     if (dst == edst) {
  397.         dstsize += MALLOC_INCR;
  398.         sdst = (char *) xrealloc((ptr_t) sdst,
  399.                      (size_t) dstsize * sizeof(char));
  400.         edst = &sdst[dstsize];
  401.         dst = &edst[-MALLOC_INCR];
  402.     }
  403.     }
  404.     *dst = 0;
  405.     return (sdst);
  406. }
  407.